package net.cyue.web;


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Date;

public class WebResult {
    public enum StatusCode {
        success(200),
        failed(201);

        private final int code;
        StatusCode(int code) {
            this.code = code;
        }

        @JsonValue
        public int getCode() {
            return this.code;
        }

        @Override
        public String toString() {
            return String.valueOf(this.code);
        }
    }

    private static ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    static {
        OBJECT_MAPPER.setVisibility(
            PropertyAccessor.ALL,
            JsonAutoDetect.Visibility.ANY
        );
    }

    public Date time = new Date();
    public WebResult.StatusCode code;
    public String message;
    public Object data;

    private WebResult(
            StatusCode code,
            String message,
            Object data
    ) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static WebResult result(
            StatusCode code
    ) {
        return WebResult.result(code, null);
    }
    public static WebResult result(
            StatusCode code,
            Object data
    ) {
        return WebResult.result(code, null, data);
    }
    public static WebResult result(
        StatusCode code,
        String message,
        Object data
    ) {
        return new WebResult(
            code,
            message,
            data
        );
    }

    public static WebResult success(
            String message
    ) {
        return WebResult.success(message, null);
    }
    public static WebResult success(
        String message,
        Object data
    ) {
        return new WebResult(
            StatusCode.success,
            message,
            data
        );
    }
    public static WebResult failed(
        String message
    ) {
        return WebResult.failed(message, null);
    }
    public static WebResult failed(
        String message,
        Object data
    ) {
        return new WebResult(
            StatusCode.failed,
            message,
            data
        );
    }

    public static String toJSON(WebResult webResult) {
        try {
            return WebResult.OBJECT_MAPPER.writeValueAsString(webResult);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
    public static String resultJSON(
            StatusCode code
    ) {
        return WebResult.toJSON(WebResult.result(code));
    }
    public static String resultJSON(
            StatusCode code,
            Object data
    ) {
        return WebResult.toJSON(WebResult.result(code, data));
    }
    public static String resultJSON(
        StatusCode code,
        String message,
        Object data
    ) {
        return WebResult.toJSON(WebResult.result(code, message, data));
    }
    public static String successJSON(
            String message
    ) {
        return WebResult.toJSON(WebResult.success(message));
    }
    public static String successJSON(
        String message,
        Object data
    ) {
        return WebResult.toJSON(WebResult.success(message, data));
    }
    public static String failedJSON(
            String message
    ) {
        return WebResult.toJSON(WebResult.failed(message));
    }
    public static String failedJSON(
        String message,
        Object data
    ) {
        return WebResult.toJSON(WebResult.failed(message, data));
    }
}

